package com.axe.processdatasync;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONArray;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private String TAG = "MyContentProvider";
    private MyHandler myHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myHandler = new MyHandler(Looper.getMainLooper(), this);
        initView();

        Log.i(TAG, "enter activity oncreate:"+android.os.Process.myPid());
    }

    private void initView() {
        findViewById(R.id.btnStartAuth).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startAuth();
            }
        });

        findViewById(R.id.btnNewProcess).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ChildProcessActivity.class);
                startActivity(intent);
            }
        });
    }

    private void startAuth() {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(2000);

                    User user = User.buildUser();

                    AuthInfo.getInstance().setAuth(true);
                    AuthInfo.getInstance().setUser(user);

                    Message ms = new Message();
                    ms.obj = user;
                    ms.what = 100;
                    myHandler.sendMessage(ms);


                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    private static class MyHandler extends Handler {

        private MainActivity activity;

        public MyHandler(@NonNull Looper looper, MainActivity activity) {
            super(looper);
            this.activity = activity;
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 100) {
                User user = (User) msg.obj;
                Toast.makeText(activity, user.getName(), Toast.LENGTH_LONG).show();
                activity.sandBroadCast(user);
            }
        }
    }

    public void sandBroadCast(User user) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", user.getName());
            jsonObject.put("gender", user.getGender());
            jsonObject.put("age", user.getAge());
            jsonObject.put("description", user.getDescription());
            jsonObject.put("isAuth",AuthInfo.getInstance().isAuth());

            JSONArray jsonArray = new JSONArray();
            for (String hobby : user.getHobbys()) {
                jsonArray.put(hobby);
            }
            jsonObject.put("hobbys", jsonArray);

            int pid = android.os.Process.myPid();
            jsonObject.put("pid", pid);

            Intent intent = new Intent();
            intent.setAction("com.axe.process.data.action");
            intent.putExtra("data", jsonObject.toString());
            sendBroadcast(intent);

        } catch (Exception e) {
            Log.e("MainActivity", e.getLocalizedMessage());
        }


    }
}